package com.example.controller.sys;


import com.alibaba.fastjson.JSONObject;
import com.example.common.Message;
import com.example.model.ums.UmsMenu;
import com.example.model.vo.UmsMenuVo;
import com.example.service.ums.UmsMenuService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/umsMenu")
@Slf4j
public class UmsMenuController {


    @Autowired
    private UmsMenuService service;


    /**
     * 查询所有菜单分页树形
     * @param vo
     * @return
     */
    @RequestMapping(value = "/findUmsMenusTreePage", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('module')")
    @ResponseBody
    public Message findUmsMenusTreePage(UmsMenuVo vo) {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.findUmsMenusTreePage 入参 UmsMenuVo ->{},start by startTime ->{}", vo , startTime);
        PageInfo<UmsMenu> umsAdmins = service.findUmsMenusTreePage(vo);
        log.info("UmsMenuController.findUmsMenusTreePage 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(umsAdmins);
    }

    /**
     * 查询所有菜单树形
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/findUmsMenusTree", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('module')")
    @ResponseBody
    public Message findUmsMenusTree(UmsMenuVo vo) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.findUmsMenusTree 入参 UmsMenuVo ->{},start by startTime ->{}", vo , startTime);
        JSONObject umsAdmins = service.findUmsMenusTree(vo);
        log.info("UmsMenuController.findUmsMenusTree 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(umsAdmins);
    }

    /**
     * 条件查询
     * @param umsMenu
     * @return
     */
    @RequestMapping(value = "/findUmsMenuByParam", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('module')")
    @ResponseBody
    public Message findUmsMenuByParam(UmsMenu umsMenu) {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.findUmsMenuByParam 入参 UmsMenu ->{},start by startTime ->{}", umsMenu, startTime);
        List<UmsMenu> umsAdmins = service.findUmsMenusByParam(umsMenu);
        log.info("UmsMenuController.findUmsMenuByParam 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(umsAdmins);
    }

    /**
     * 新增
     * @param umsMenu
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('module')")
    public Message insert(@RequestBody UmsMenu umsMenu) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.insert 入参 UmsMenu ->{},start by startTime ->{}", umsMenu, startTime);
        if (null == umsMenu) {
            throw new Exception("参数异常.");
        }
        Integer rlt = service.insert(umsMenu);
        log.info("UmsMenuController.insert 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(rlt);
    }

    /**
     * 修改
     * @param umsMenu
     * @return
     */
    @RequestMapping(value = "/updateMenuByParam", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('module')")
    public Message updateMenuByParam(@RequestBody UmsMenu umsMenu) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.updateMenuByParam 入参 UmsMenu ->{},start by startTime ->{}", umsMenu, startTime);
        if (null == umsMenu) {
            throw new Exception("参数异常.");
        }
        Integer rlt = service.updateMenuByParam(umsMenu);
        log.info("UmsMenuController.updateMenuByParam 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(rlt);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('module')")
    public Message deleteById(@PathVariable("id") String id) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsMenuController.deleteById 入参 id ->{},start by startTime ->{}", id, startTime);
        if (null == id) {
            throw new Exception("参数异常.");
        }

        try {
            service.deleteById(id);
            return Message.ok();
        } catch (Exception e) {
            return Message.error(500,e.getMessage());
        } finally {
            log.info("UmsMenuController.deleteById 耗时：{} ms", System.currentTimeMillis() - startTime);

        }

    }


}
